﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Topro.Application.Interface;
using Topro.Entity.Query;
using Topro.Util;

namespace Topro.Admin.Controllers
{
    /// <summary>
    /// OEE控制器
    /// </summary>
    public class OEEController : Controller
    {
        #region 注入
        /// <summary>
        /// 设备数据接口
        /// </summary>
        private readonly DeviceDataService _deviceDataService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceDataService">设备数据接口</param>
        public OEEController(DeviceDataService deviceDataService)
        {
            _deviceDataService = deviceDataService;
        }
        #endregion

        public IActionResult Index()
        {
            ViewBag.StartTime = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd");
            ViewBag.EndTime = DateTime.Now.ToString("yyyy-MM-dd");
            return View();
        }

        /// <summary>
        /// 搜索时间叠状图分析
        /// </summary>
        /// <param name="query">参数</param>
        public JsonResult TimeAnalysisQuery(TimeAnalysisQuery query)
        {
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (DateUtil.IsDateTime(query.StartTime) && DateUtil.IsDateTime(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd 23:59:59");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            //开始时间
            var startTime = Convert.ToDateTime(query.StartTime);
            //获取最后一天
            var lastDay = startTime.AddDays(1 - startTime.Day).AddMonths(1).AddDays(-1);
            //结束时间
            var endTime = Convert.ToDateTime(query.EndTime);
            //获取最后一天
            var firtDay = endTime.AddDays(1 - endTime.Day);
            //天
            var days = new List<DateTime>();

            if (startTime.Day == endTime.Day)
            {
                days.Add(startTime.Date);
            }
            else if (startTime.Day < endTime.Day)
            {
                for (int i = startTime.Day; i <= endTime.Day; i++)
                {
                    days.Add(Convert.ToDateTime(startTime.ToString($"yyyy-MM-{i.ToString("00")}")));
                }
            }
            else if (startTime.Day > endTime.Day)
            {
                for (int i = startTime.Day; i <= lastDay.Day; i++)
                {
                    days.Add(Convert.ToDateTime(startTime.ToString($"yyyy-MM-{i.ToString("00")}")));
                }

                for (int j = firtDay.Day; j <= endTime.Day; j++)
                {
                    days.Add(Convert.ToDateTime(endTime.ToString($"yyyy-MM-{j.ToString("00")}")));
                }
            }

            query.Days = days;
            //获取时间分析
            var response = _deviceDataService.TimeAnalysis(query);

            return Json(response);
        }
    }
}
